import { Injectable } from '@angular/core';
import { Http, Headers } from "@angular/http";
import { Setofvalue } from "../entity/setofvalue.model";
import { Observable } from "rxjs/Observable";
import { AppConfig } from "../config";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class SetofvalueService { 
  private  param:string = "dicValues";
  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService) { }

   getSetofValues(): Promise<Setofvalue[]> {   
    return this.http.get(`${this.url}`)
      .toPromise()
      .then(response => response.json().data as Setofvalue[])
      .catch(this.handleError);
  }

  getSetofValuesBykeyID(keyID: number): Promise<Setofvalue[]> {
    return this.http.get(`${this.url}/keyId/${keyID}`)
      .toPromise()
      .then(response => response.json().data as Setofvalue[])
      .catch(this.handleError);
  }

  create(setOfValue: Setofvalue): Promise<Setofvalue> {
    return this.http
      .post(this.url, JSON.stringify(setOfValue))
      .toPromise()
      .then(res => {
        try {
          let result = res.json();
          switch (result.code) {
            case "200":
              return result.data as Setofvalue;
            case "-1":
              throw new Error(result.message);
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }
  update(setofvalue: Setofvalue): Promise<Setofvalue> {
    const url = `${this.url}`;
    return this.http
      .put(url, JSON.stringify(setofvalue))
      .toPromise()
      .then(() => setofvalue)
      .catch(this.handleError);
  }

  hasValueCode(valueCode: string, valueId: number, keyId: number): Observable<boolean> {
    const url = `${this.url}/valueCode/${valueCode}/valueId/${valueId}/keyId/${keyId}`;
    return this.http.get(url).map(response => {
      let code = response.json().code;
      switch (code) {
        case "3001":
          return true;
        case "3002":
          return false;
      }

    }).catch(this.handleError);
  }
  hasValueDisplay(valueDisplay: string, valueId: number, keyId: number): Observable<boolean> {
    const url = `${this.url}/valueDisplay/${valueDisplay}/valueId/${valueId}/keyId/${keyId}`;
    return this.http.get(url).map(response => {
      let code = response.json().code;
      switch (code) {
        case "3001":
          return true;
        case "3002":
          return false;
      }
    }).catch(this.handleError);
  }
  delete(id: number): Promise<void> {
    const url = `${this.url}/${id}`;
    return this.http.delete(url)
      .toPromise()
      .then(() => null)
      .catch(this.handleError);
  }
  batchDelete(valueIds: number[]): Promise<boolean> {
    const apiUrl = `${this.url}?items=${valueIds}`;
    return this.http.delete(apiUrl)
      .toPromise()
      .then(response => {
        try {
          let result = response.json();
          switch (result.code) {
            case "200":
              return true;
            case "-1":
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }


  private handleError(error: any): Promise<any> {
    // console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }

}



// WEBPACK FOOTER //
// ./src/app/service/setofvalue.service.ts